package gui.testSetup.components;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * 
 * @author William Whitney
 */
public class PageInputEditor extends JPanel
{

    private Object pageValue;
    private TestConfSetting currPage;
    private File directoryPath;
    private static final String srcDirWarningMessage = "The selected file is not located in the src directory!";
    private static final String srcDirWarningTitle = "Warning";


    public PageInputEditor(TestConfSetting enumPageType)
    {
        currPage = enumPageType;

        buildPanel(enumPageType);
    }
    
    public PageInputEditor(TestConfSetting enumPageType, File directoryPath)
    {
        currPage = enumPageType;
        this.directoryPath = directoryPath;

        buildPanel(enumPageType);
    }

    public void setPage(TestConfSetting enumPageType)
    {
        currPage = enumPageType;
        buildPanel(enumPageType);

    }

    public String getValue()
    {
        if (currPage == TestConfSetting.CODE_LOCATION)
        {
            return ((JTextField) pageValue).getText();
        }
        else
        {
            return ((JTextArea) pageValue).getText();
        }

    }

    public void setValue(String value)
    {
        if (currPage == TestConfSetting.CODE_LOCATION)
        {
            ((JTextField) pageValue).setText(value);
        }
        else
        {
            ((JTextArea) pageValue).setText(value);
        }
    }

    private void buildPanel(TestConfSetting enumPageType)
    {
        switch (enumPageType)
        {
            case CODE_LOCATION:
                this.removeAll();
                this.setLayout(new BorderLayout());
                this.add(getFileInputPane(), BorderLayout.NORTH);
                break;
            default:

                this.removeAll();
                this.setLayout(new BorderLayout());
                this.add(getTextInputPane(), BorderLayout.CENTER);

                break;
        }
    }

    private JPanel getTextInputPane()
    {
        JPanel panel = new JPanel();

        TitledBorder border = new TitledBorder("Input " + currPage.getTitle());
        panel.setBorder(border);

        panel.setLayout(new BorderLayout());

        pageValue = new JTextArea();
        JScrollPane sPane = new JScrollPane((JTextArea) pageValue);

        panel.add(sPane, BorderLayout.CENTER);

        return panel;
    }

    /**
     * Returns a pane with a file input box on it.
     */
    private JPanel getFileInputPane()
    {


        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        TitledBorder border = new TitledBorder("Select Source File");
        panel.setBorder(border);

        //Label
        JLabel fileLocation = new JLabel("File Location:");
        panel.add(fileLocation, BorderLayout.WEST);

        //Add box to show file location
        pageValue = new JTextField();
        ((JTextField) pageValue).setColumns(40);
        panel.add((JTextField) pageValue, BorderLayout.CENTER);

        //Button
        JButton selectFile = new JButton("Select File");
        selectFile.addActionListener(getOpenButtonListener());
        panel.add(selectFile, BorderLayout.EAST);

        return panel;
    }

    public ActionListener getOpenButtonListener()
    {
        return new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {
                //File Chooser
                JFileChooser chooser = new JFileChooser(directoryPath);
                FileNameExtensionFilter filter = new FileNameExtensionFilter("Java Source File", "java");
                chooser.setFileFilter(filter);

                int returnVal = chooser.showOpenDialog(null);

                if (returnVal == JFileChooser.APPROVE_OPTION)
                {
                    ((JTextField) pageValue).setText(chooser.getSelectedFile().getAbsolutePath());
                    directoryPath = chooser.getCurrentDirectory();
                    
                    if (!directoryPath.getAbsolutePath().contains("src")) {
                        JOptionPane.showMessageDialog(chooser, srcDirWarningMessage, srcDirWarningTitle, JOptionPane.WARNING_MESSAGE);
                    }
                }
            }
        };
    }
    
    public void addDocumentListener(DocumentListener documentListener) {
        ((JTextArea) pageValue).getDocument().addDocumentListener(documentListener);
    }
    
    public File getLastVisitedDirectory() {
        return directoryPath;
    }
}
